 /*
 * UMLSynch - VPSDE plugin part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.vpsde.rpc.VPSDEDiagramManager;

import java.util.Iterator;

import com.vp.plugin.ApplicationManager;
import com.vp.plugin.DiagramManager;
import com.vp.plugin.diagram.IDiagramElement;
import com.vp.plugin.diagram.IDiagramUIModel;
import com.vp.plugin.model.IModelElement;
import com.vp.plugin.model.IPackage;
import com.vp.plugin.model.factory.IModelElementFactory;

public class VPPackageDiagramManager {
	private static VPPackageDiagramManager Singletone = null;
	
	public static String PROJECT_NAME = "PROJECT_NAME";
	public static String PACKAGE_PATH = "PACKAGE_PATH";
	public static String PACKAGE_DEPENDENCY = "PACKAGE_DEP";	
	
	public static VPPackageDiagramManager instance() {
		if (Singletone == null) {
			Singletone = new VPPackageDiagramManager();
		}
		return Singletone;
	}
	
	private IDiagramElement FindPackageOnDiagram(IDiagramUIModel dm, String packName)
	{
		Iterator dmIter = dm.diagramElementIterator();
		
		while (dmIter.hasNext()) {
			IDiagramElement de = (IDiagramElement) dmIter.next();
			IModelElement me = de.getModelElement();
			
			if ((me instanceof IPackage) && (me.getName().equals(packName))) {
				return de;
			}			
		}		

		return null;		
	}
	
	public IDiagramElement AddPackageToDiagram(IDiagramUIModel diagramModel, String packName)
	{	
	  DiagramManager diagramManager = ApplicationManager.instance().getDiagramManager();
	  
	  IDiagramElement diagramElement = FindPackageOnDiagram(diagramModel, packName);
	  diagramModel.setAutoFitShapesSize(true);
	  diagramModel.setAlignToGrid(true);
	  
	  IPackage packModelElement = null;				
	
	  if (diagramElement == null)
      {
		  packModelElement = IModelElementFactory.instance().createPackage(); 
		  packModelElement.setName(packName);
		  
		  diagramElement = diagramManager.createDiagramElement(diagramModel, packModelElement);
		  
		  diagramElement.getModelElement().setName(packName);
      }
	  
	  return diagramElement;
	}

	public String GetProperty(IDiagramElement diagramElement, String prop)
	{	
		String val = null;
		if ((diagramElement == null) || (diagramElement.getModelElement() == null))
			return null;
        return VPPropertyManager.instance().GetProperty(diagramElement.getModelElement(), prop);		
	}

	public void SetProperty(IDiagramElement diagramElement, String prop, String propValue)
	{	
		String val = null;
		if ((diagramElement == null) || (diagramElement.getModelElement() == null))
			return;
        VPPropertyManager.instance().
        	SetProperty(diagramElement.getModelElement(), prop, propValue);		
	}

}
